/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador.autogenerats;

import controlador.autogenerats.exceptions.IllegalOrphanException;
import controlador.autogenerats.exceptions.NonexistentEntityException;
import entity.Produccio;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entity.UnitatsMesura;
import entity.Camp;
import entity.Producte;
import entity.Tractament;
import java.util.ArrayList;
import java.util.Collection;
import entity.Collita;
import entity.Abonat;

/**
 *
 * @author David
 */
public class ProduccioJpaController implements Serializable {

    public ProduccioJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Produccio produccio) {
        if (produccio.getTractamentCollection() == null) {
            produccio.setTractamentCollection(new ArrayList<Tractament>());
        }
        if (produccio.getCollitaCollection() == null) {
            produccio.setCollitaCollection(new ArrayList<Collita>());
        }
        if (produccio.getAbonatCollection() == null) {
            produccio.setAbonatCollection(new ArrayList<Abonat>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            UnitatsMesura unitatsMesuraId = produccio.getUnitatsMesuraId();
            if (unitatsMesuraId != null) {
                unitatsMesuraId = em.getReference(unitatsMesuraId.getClass(), unitatsMesuraId.getId());
                produccio.setUnitatsMesuraId(unitatsMesuraId);
            }
            Camp campId = produccio.getCampId();
            if (campId != null) {
                campId = em.getReference(campId.getClass(), campId.getId());
                produccio.setCampId(campId);
            }
            Producte productesId = produccio.getProductesId();
            if (productesId != null) {
                productesId = em.getReference(productesId.getClass(), productesId.getId());
                produccio.setProductesId(productesId);
            }
            Collection<Tractament> attachedTractamentCollection = new ArrayList<Tractament>();
            for (Tractament tractamentCollectionTractamentToAttach : produccio.getTractamentCollection()) {
                tractamentCollectionTractamentToAttach = em.getReference(tractamentCollectionTractamentToAttach.getClass(), tractamentCollectionTractamentToAttach.getId());
                attachedTractamentCollection.add(tractamentCollectionTractamentToAttach);
            }
            produccio.setTractamentCollection(attachedTractamentCollection);
            Collection<Collita> attachedCollitaCollection = new ArrayList<Collita>();
            for (Collita collitaCollectionCollitaToAttach : produccio.getCollitaCollection()) {
                collitaCollectionCollitaToAttach = em.getReference(collitaCollectionCollitaToAttach.getClass(), collitaCollectionCollitaToAttach.getId());
                attachedCollitaCollection.add(collitaCollectionCollitaToAttach);
            }
            produccio.setCollitaCollection(attachedCollitaCollection);
            Collection<Abonat> attachedAbonatCollection = new ArrayList<Abonat>();
            for (Abonat abonatCollectionAbonatToAttach : produccio.getAbonatCollection()) {
                abonatCollectionAbonatToAttach = em.getReference(abonatCollectionAbonatToAttach.getClass(), abonatCollectionAbonatToAttach.getId());
                attachedAbonatCollection.add(abonatCollectionAbonatToAttach);
            }
            produccio.setAbonatCollection(attachedAbonatCollection);
            em.persist(produccio);
            if (unitatsMesuraId != null) {
                unitatsMesuraId.getProduccioCollection().add(produccio);
                unitatsMesuraId = em.merge(unitatsMesuraId);
            }
            if (campId != null) {
                campId.getProduccioCollection().add(produccio);
                campId = em.merge(campId);
            }
            if (productesId != null) {
                productesId.getProduccioCollection().add(produccio);
                productesId = em.merge(productesId);
            }
            for (Tractament tractamentCollectionTractament : produccio.getTractamentCollection()) {
                Produccio oldProduccioIdOfTractamentCollectionTractament = tractamentCollectionTractament.getProduccioId();
                tractamentCollectionTractament.setProduccioId(produccio);
                tractamentCollectionTractament = em.merge(tractamentCollectionTractament);
                if (oldProduccioIdOfTractamentCollectionTractament != null) {
                    oldProduccioIdOfTractamentCollectionTractament.getTractamentCollection().remove(tractamentCollectionTractament);
                    oldProduccioIdOfTractamentCollectionTractament = em.merge(oldProduccioIdOfTractamentCollectionTractament);
                }
            }
            for (Collita collitaCollectionCollita : produccio.getCollitaCollection()) {
                Produccio oldProduccioIdOfCollitaCollectionCollita = collitaCollectionCollita.getProduccioId();
                collitaCollectionCollita.setProduccioId(produccio);
                collitaCollectionCollita = em.merge(collitaCollectionCollita);
                if (oldProduccioIdOfCollitaCollectionCollita != null) {
                    oldProduccioIdOfCollitaCollectionCollita.getCollitaCollection().remove(collitaCollectionCollita);
                    oldProduccioIdOfCollitaCollectionCollita = em.merge(oldProduccioIdOfCollitaCollectionCollita);
                }
            }
            for (Abonat abonatCollectionAbonat : produccio.getAbonatCollection()) {
                Produccio oldProduccioIdOfAbonatCollectionAbonat = abonatCollectionAbonat.getProduccioId();
                abonatCollectionAbonat.setProduccioId(produccio);
                abonatCollectionAbonat = em.merge(abonatCollectionAbonat);
                if (oldProduccioIdOfAbonatCollectionAbonat != null) {
                    oldProduccioIdOfAbonatCollectionAbonat.getAbonatCollection().remove(abonatCollectionAbonat);
                    oldProduccioIdOfAbonatCollectionAbonat = em.merge(oldProduccioIdOfAbonatCollectionAbonat);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Produccio produccio) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Produccio persistentProduccio = em.find(Produccio.class, produccio.getId());
            UnitatsMesura unitatsMesuraIdOld = persistentProduccio.getUnitatsMesuraId();
            UnitatsMesura unitatsMesuraIdNew = produccio.getUnitatsMesuraId();
            Camp campIdOld = persistentProduccio.getCampId();
            Camp campIdNew = produccio.getCampId();
            Producte productesIdOld = persistentProduccio.getProductesId();
            Producte productesIdNew = produccio.getProductesId();
            Collection<Tractament> tractamentCollectionOld = persistentProduccio.getTractamentCollection();
            Collection<Tractament> tractamentCollectionNew = produccio.getTractamentCollection();
            Collection<Collita> collitaCollectionOld = persistentProduccio.getCollitaCollection();
            Collection<Collita> collitaCollectionNew = produccio.getCollitaCollection();
            Collection<Abonat> abonatCollectionOld = persistentProduccio.getAbonatCollection();
            Collection<Abonat> abonatCollectionNew = produccio.getAbonatCollection();
            List<String> illegalOrphanMessages = null;
            for (Tractament tractamentCollectionOldTractament : tractamentCollectionOld) {
                if (!tractamentCollectionNew.contains(tractamentCollectionOldTractament)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Tractament " + tractamentCollectionOldTractament + " since its produccioId field is not nullable.");
                }
            }
            for (Collita collitaCollectionOldCollita : collitaCollectionOld) {
                if (!collitaCollectionNew.contains(collitaCollectionOldCollita)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Collita " + collitaCollectionOldCollita + " since its produccioId field is not nullable.");
                }
            }
            for (Abonat abonatCollectionOldAbonat : abonatCollectionOld) {
                if (!abonatCollectionNew.contains(abonatCollectionOldAbonat)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Abonat " + abonatCollectionOldAbonat + " since its produccioId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (unitatsMesuraIdNew != null) {
                unitatsMesuraIdNew = em.getReference(unitatsMesuraIdNew.getClass(), unitatsMesuraIdNew.getId());
                produccio.setUnitatsMesuraId(unitatsMesuraIdNew);
            }
            if (campIdNew != null) {
                campIdNew = em.getReference(campIdNew.getClass(), campIdNew.getId());
                produccio.setCampId(campIdNew);
            }
            if (productesIdNew != null) {
                productesIdNew = em.getReference(productesIdNew.getClass(), productesIdNew.getId());
                produccio.setProductesId(productesIdNew);
            }
            Collection<Tractament> attachedTractamentCollectionNew = new ArrayList<Tractament>();
            for (Tractament tractamentCollectionNewTractamentToAttach : tractamentCollectionNew) {
                tractamentCollectionNewTractamentToAttach = em.getReference(tractamentCollectionNewTractamentToAttach.getClass(), tractamentCollectionNewTractamentToAttach.getId());
                attachedTractamentCollectionNew.add(tractamentCollectionNewTractamentToAttach);
            }
            tractamentCollectionNew = attachedTractamentCollectionNew;
            produccio.setTractamentCollection(tractamentCollectionNew);
            Collection<Collita> attachedCollitaCollectionNew = new ArrayList<Collita>();
            for (Collita collitaCollectionNewCollitaToAttach : collitaCollectionNew) {
                collitaCollectionNewCollitaToAttach = em.getReference(collitaCollectionNewCollitaToAttach.getClass(), collitaCollectionNewCollitaToAttach.getId());
                attachedCollitaCollectionNew.add(collitaCollectionNewCollitaToAttach);
            }
            collitaCollectionNew = attachedCollitaCollectionNew;
            produccio.setCollitaCollection(collitaCollectionNew);
            Collection<Abonat> attachedAbonatCollectionNew = new ArrayList<Abonat>();
            for (Abonat abonatCollectionNewAbonatToAttach : abonatCollectionNew) {
                abonatCollectionNewAbonatToAttach = em.getReference(abonatCollectionNewAbonatToAttach.getClass(), abonatCollectionNewAbonatToAttach.getId());
                attachedAbonatCollectionNew.add(abonatCollectionNewAbonatToAttach);
            }
            abonatCollectionNew = attachedAbonatCollectionNew;
            produccio.setAbonatCollection(abonatCollectionNew);
            produccio = em.merge(produccio);
            if (unitatsMesuraIdOld != null && !unitatsMesuraIdOld.equals(unitatsMesuraIdNew)) {
                unitatsMesuraIdOld.getProduccioCollection().remove(produccio);
                unitatsMesuraIdOld = em.merge(unitatsMesuraIdOld);
            }
            if (unitatsMesuraIdNew != null && !unitatsMesuraIdNew.equals(unitatsMesuraIdOld)) {
                unitatsMesuraIdNew.getProduccioCollection().add(produccio);
                unitatsMesuraIdNew = em.merge(unitatsMesuraIdNew);
            }
            if (campIdOld != null && !campIdOld.equals(campIdNew)) {
                campIdOld.getProduccioCollection().remove(produccio);
                campIdOld = em.merge(campIdOld);
            }
            if (campIdNew != null && !campIdNew.equals(campIdOld)) {
                campIdNew.getProduccioCollection().add(produccio);
                campIdNew = em.merge(campIdNew);
            }
            if (productesIdOld != null && !productesIdOld.equals(productesIdNew)) {
                productesIdOld.getProduccioCollection().remove(produccio);
                productesIdOld = em.merge(productesIdOld);
            }
            if (productesIdNew != null && !productesIdNew.equals(productesIdOld)) {
                productesIdNew.getProduccioCollection().add(produccio);
                productesIdNew = em.merge(productesIdNew);
            }
            for (Tractament tractamentCollectionNewTractament : tractamentCollectionNew) {
                if (!tractamentCollectionOld.contains(tractamentCollectionNewTractament)) {
                    Produccio oldProduccioIdOfTractamentCollectionNewTractament = tractamentCollectionNewTractament.getProduccioId();
                    tractamentCollectionNewTractament.setProduccioId(produccio);
                    tractamentCollectionNewTractament = em.merge(tractamentCollectionNewTractament);
                    if (oldProduccioIdOfTractamentCollectionNewTractament != null && !oldProduccioIdOfTractamentCollectionNewTractament.equals(produccio)) {
                        oldProduccioIdOfTractamentCollectionNewTractament.getTractamentCollection().remove(tractamentCollectionNewTractament);
                        oldProduccioIdOfTractamentCollectionNewTractament = em.merge(oldProduccioIdOfTractamentCollectionNewTractament);
                    }
                }
            }
            for (Collita collitaCollectionNewCollita : collitaCollectionNew) {
                if (!collitaCollectionOld.contains(collitaCollectionNewCollita)) {
                    Produccio oldProduccioIdOfCollitaCollectionNewCollita = collitaCollectionNewCollita.getProduccioId();
                    collitaCollectionNewCollita.setProduccioId(produccio);
                    collitaCollectionNewCollita = em.merge(collitaCollectionNewCollita);
                    if (oldProduccioIdOfCollitaCollectionNewCollita != null && !oldProduccioIdOfCollitaCollectionNewCollita.equals(produccio)) {
                        oldProduccioIdOfCollitaCollectionNewCollita.getCollitaCollection().remove(collitaCollectionNewCollita);
                        oldProduccioIdOfCollitaCollectionNewCollita = em.merge(oldProduccioIdOfCollitaCollectionNewCollita);
                    }
                }
            }
            for (Abonat abonatCollectionNewAbonat : abonatCollectionNew) {
                if (!abonatCollectionOld.contains(abonatCollectionNewAbonat)) {
                    Produccio oldProduccioIdOfAbonatCollectionNewAbonat = abonatCollectionNewAbonat.getProduccioId();
                    abonatCollectionNewAbonat.setProduccioId(produccio);
                    abonatCollectionNewAbonat = em.merge(abonatCollectionNewAbonat);
                    if (oldProduccioIdOfAbonatCollectionNewAbonat != null && !oldProduccioIdOfAbonatCollectionNewAbonat.equals(produccio)) {
                        oldProduccioIdOfAbonatCollectionNewAbonat.getAbonatCollection().remove(abonatCollectionNewAbonat);
                        oldProduccioIdOfAbonatCollectionNewAbonat = em.merge(oldProduccioIdOfAbonatCollectionNewAbonat);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = produccio.getId();
                if (findProduccio(id) == null) {
                    throw new NonexistentEntityException("The produccio with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Produccio produccio;
            try {
                produccio = em.getReference(Produccio.class, id);
                produccio.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The produccio with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Tractament> tractamentCollectionOrphanCheck = produccio.getTractamentCollection();
            for (Tractament tractamentCollectionOrphanCheckTractament : tractamentCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Produccio (" + produccio + ") cannot be destroyed since the Tractament " + tractamentCollectionOrphanCheckTractament + " in its tractamentCollection field has a non-nullable produccioId field.");
            }
            Collection<Collita> collitaCollectionOrphanCheck = produccio.getCollitaCollection();
            for (Collita collitaCollectionOrphanCheckCollita : collitaCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Produccio (" + produccio + ") cannot be destroyed since the Collita " + collitaCollectionOrphanCheckCollita + " in its collitaCollection field has a non-nullable produccioId field.");
            }
            Collection<Abonat> abonatCollectionOrphanCheck = produccio.getAbonatCollection();
            for (Abonat abonatCollectionOrphanCheckAbonat : abonatCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Produccio (" + produccio + ") cannot be destroyed since the Abonat " + abonatCollectionOrphanCheckAbonat + " in its abonatCollection field has a non-nullable produccioId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            UnitatsMesura unitatsMesuraId = produccio.getUnitatsMesuraId();
            if (unitatsMesuraId != null) {
                unitatsMesuraId.getProduccioCollection().remove(produccio);
                unitatsMesuraId = em.merge(unitatsMesuraId);
            }
            Camp campId = produccio.getCampId();
            if (campId != null) {
                campId.getProduccioCollection().remove(produccio);
                campId = em.merge(campId);
            }
            Producte productesId = produccio.getProductesId();
            if (productesId != null) {
                productesId.getProduccioCollection().remove(produccio);
                productesId = em.merge(productesId);
            }
            em.remove(produccio);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Produccio> findProduccioEntities() {
        return findProduccioEntities(true, -1, -1);
    }

    public List<Produccio> findProduccioEntities(int maxResults, int firstResult) {
        return findProduccioEntities(false, maxResults, firstResult);
    }

    private List<Produccio> findProduccioEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Produccio.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Produccio findProduccio(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Produccio.class, id);
        } finally {
            em.close();
        }
    }

    public int getProduccioCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Produccio> rt = cq.from(Produccio.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
